<template>
  <div class="app-container">
    <el-card shadow="never" style="margin-bottom: 15px">
      <i class="el-icon-info" />
      <span>帖子标题：{{ issueInfo.issueTitle }}</span>
      <el-button style="padding: 3px 0; float: right" type="text" @click="goBack()"><i class="el-icon-arrow-left" />返回帖子列表</el-button>
    </el-card>

    <el-card shadow="never" style="margin-bottom: 15px">
      <i class="el-icon-tickets" />
      <span>数据列表</span>
      <span style="font-size: 15px; font-weight: bold">总回帖人数：</span>
      <span style="font-size: 15px; font-weight: normal; margin-right: 20px">{{ replyUserCount }}</span>
      <el-button style="float: right" size="mini" @click="handleExportUserList()">导出回帖人员列表</el-button>
    </el-card>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      max-height="690"
      highlight-current-row
      style="width: 100%;"
      :row-style="{height:'32px'}"
      tooltip-effect="light"
      @sort-change="sortChange"
    >
      <el-table-column label="编号" align="center" fixed width="100px" prop="postId" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.postId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="center">
        <template slot-scope="{row}">
          <el-popover
            placement="right-start"
            width="1000"
            trigger="click"
          >
            <div style="margin: 10px;">
              <el-row>
                <span class="middle-align"><el-image :key="row.headImg" :src="row.headImg" class="user-avatar" /></span>
                <span v-if="row.nickName" class="nickname middle-align">{{ row.nickName }}</span>
                <span v-else class="nickname middle-align">[暂未设置昵称]</span>
              </el-row>
              <el-row>
                <div v-if="row.content" class="issue-content" v-html="row.content" />
                <div v-else class="issue-content">[无内容]</div>
              </el-row>
            </div>
            <el-button slot="reference" size="mini">查看</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="用户ID" align="center" prop="userId" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.userId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" show-overflow-tooltip>
        <template slot-scope="{row}">
          <span>{{ row.realName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否匿名" align="center" prop="userAnonymous" sortable="custom">
        <template slot-scope="{row}">
          <el-tag v-if="row.userAnonymous===0" size="mini" type="success">不匿名</el-tag>
          <el-tag v-if="row.userAnonymous===1" size="mini" type="info">匿名</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="点赞量" align="center" prop="likeNum" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.likeNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="评论量" align="center" prop="commentNum" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.commentNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="isPass" sortable="custom">
        <template slot-scope="{row}">
          <el-tag v-if="row.isPass===0" size="mini" type="primary">未审核</el-tag>
          <el-tag v-if="row.isPass===1" size="mini" type="success">通过</el-tag>
          <el-tag v-if="row.isPass===2" size="mini" type="danger">不通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="回帖时间" align="center" width="150px" prop="createTime" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="250px" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleComment(row.postId)">
            评论
          </el-button>
          <el-button type="success" size="mini" @click="handlePass(row)">
            审核
          </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row.postId)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page-num.sync="listQuery.pageNum" :page-size.sync="listQuery.pageSize" @pagination="showPostList" />

    <el-dialog title="回帖评论" :visible.sync="dialogCommentVisible" width="1100px" top="50px">
      <el-table
        :key="commentListQuery.postId"
        v-loading="commentListLoading"
        :data="commentList"
        border
        fit
        max-height="600"
        highlight-current-row
        style="width: 100%;"
        :row-style="{height:'32px'}"
        tooltip-effect="light"
      >
        <el-table-column label="编号" align="center" fixed width="80px">
          <template slot-scope="{row}">
            <span>{{ row.commentId }}</span>
          </template>
        </el-table-column>
        <el-table-column label="用户ID" align="center" width="100px">
          <template slot-scope="{row}">
            <span>{{ row.userId }}</span>
          </template>
        </el-table-column>
        <el-table-column label="姓名" align="center" show-overflow-tooltip width="150px">
          <template slot-scope="{row}">
            <span>{{ row.realName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="评论内容" align="center" show-overflow-tooltip>
          <template slot-scope="{row}">
            <span>{{ row.comment }}</span>
          </template>
        </el-table-column>
        <el-table-column label="点赞量" align="center" width="100px">
          <template slot-scope="{row}">
            <span>{{ row.likeNum }}</span>
          </template>
        </el-table-column>
        <el-table-column label="评论时间" align="center" width="150px">
          <template slot-scope="{row}">
            <span>{{ row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120px" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="{row}">
            <el-button type="danger" size="mini" @click="handleCommentDelete(row.commentId)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="commentTotal>0" :total="commentTotal" :page-num.sync="commentListQuery.pageNum" :page-size.sync="commentListQuery.pageSize" @pagination="showCommentList" />
    </el-dialog>

    <el-dialog title="审核回帖" :visible.sync="dialogPassVisible" width="500px">
      <el-form ref="passForm" :rules="passRules" :model="tempPass" label-width="130px">
        <el-form-item label="审核结果" prop="isPass">
          <el-radio v-model="tempPass.isPass" :label="1">通过</el-radio>
          <el-radio v-model="tempPass.isPass" :label="2">不通过</el-radio>
        </el-form-item>
        <p style="margin-left: 50px">注：若审核不通过，该回帖将被隐藏！</p>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogPassVisible = false">
          关闭
        </el-button>
        <el-button type="primary" @click="doPass">
          确认审核
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  showPostList,
  getIssueDetail,
  deletePost,
  passPost,
  getPostCommentList,
  deleteComment
} from '@/api/discuss'
import { countReplyNumber, exportReplyUserList } from '@/api/activity'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { isInteger } from '@/utils/validate'

export default {
  name: 'Post',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      issueId: undefined,
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      dialogLoading: false,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        sortProp: '',
        sortOrder: '',
        issueId: undefined
      },
      dialogCommentVisible: false,
      dialogPassVisible: false,
      issueInfo: {},
      tempPass: {
        postId: undefined,
        isPass: undefined
      },
      passRules: {
        isPass: [{ required: true, message: '请选择审核结果', trigger: 'change' }, { validator: isInteger, trigger: 'blur' }]
      },
      // 评论列表
      commentList: [],
      commentTotal: 0,
      commentListLoading: true,
      commentListQuery: {
        pageNum: 1,
        pageSize: 20,
        postId: undefined
      },
      tempPostId: undefined,
      replyUserCount: 0
    }
  },
  created() {
    this.issueId = this.$route.params.id
    this.countReplyNumber()
    this.getIssueInfo()
    this.showPostList()
  },
  methods: {
    countReplyNumber() {
      countReplyNumber(this.issueId).then(response => {
        this.replyUserCount = response.data
      })
    },
    handleExportUserList() {
      const loading = this.$loading({
        lock: true,
        text: '正在导出中，请耐心等待',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      exportReplyUserList(this.issueId).then((response) => {
        this.$notify({
          title: '回复人员名单导出成功',
          dangerouslyUseHTMLString: true,
          message: '<p>已生成回复人员名单，请下载：</p>' +
            '<a target="_blank" href="' + response.data + '">' +
            '<strong style="text-decoration: underline">点击下载</strong>' +
            '</a>',
          type: 'success',
          duration: 0
        })
        loading.close()
      })
    },
    resetCommentDialog() {
      this.commentList = []
      this.commentTotal = 0
      this.commentListQuery = {
        pageNum: 1,
        pageSize: 20,
        postId: this.tempPostId
      }
    },
    showCommentList() {
      this.commentListLoading = true
      this.commentListQuery.postId = this.tempPostId
      getPostCommentList(this.commentListQuery).then(response => {
        this.commentList = response.data.list
        this.commentTotal = response.data.total
        this.commentListLoading = false
      })
    },
    handleComment(postId) {
      this.tempPostId = postId
      this.resetCommentDialog()
      this.dialogCommentVisible = true
      this.showCommentList()
    },
    handleCommentDelete(commentId) {
      this.$confirm('此删除操作不可逆，是否继续删除评论?', '提示', {
        confirmButtonText: '确认删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteComment(commentId).then(() => {
          this.showCommentList()
          this.$notify({
            title: 'Success',
            message: '删除评论成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    },
    showPostList() {
      this.listLoading = true
      this.listQuery.issueId = this.issueId
      showPostList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    getIssueInfo() {
      getIssueDetail(this.issueId).then(response => {
        this.issueInfo = response.data
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.showPostList()
    },
    sortChange(data) {
      const { prop, order } = data
      this.listQuery.sortProp = prop
      if (order === 'ascending') {
        this.listQuery.sortOrder = '1'
      } else {
        this.listQuery.sortOrder = '-1'
      }
      this.handleFilter()
    },
    resetDetailDialog() {
      this.row = {}
    },
    resetDialog() {
      this.tempHandle = {
        applyId: undefined,
        progress: undefined,
        handleNote: ''
      }
    },
    handleDetail(row) {
      this.resetDetailDialog()
      this.row = Object.assign({}, row)
      this.dialogDetailVisible = true
    },
    handlePass(row) {
      this.tempPass = {
        postId: row.postId,
        isPass: row.isPass
      }
      this.dialogPassVisible = true
    },
    doPass() {
      this.$refs['passForm'].validate((valid) => {
        if (valid) {
          passPost(this.tempPass).then(response => {
            this.showPostList()
            this.dialogPassVisible = false
            this.$notify({
              title: 'Success',
              message: '审核回帖成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleDelete(postId) {
      this.$confirm('此删除操作不可逆，该回帖下的所有评论将无法查看，是否继续?', '提示', {
        confirmButtonText: '确认删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePost(postId).then(() => {
          this.showPostList()
          this.$notify({
            title: 'Success',
            message: '删除回帖成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    },
    goBack() {
      if (this.$route.params.type === 1) {
        this.$router.push({
          name: 'ForumIssue'
        })
      } else {
        this.$router.push({
          name: 'LeagueIssue'
        })
      }
    }
  }
}
</script>

<style scoped>
  .col-title {
    text-align: right
  }

  .col-title p {
    font-weight: bolder;
  }

  .col-content p {
    color: #848383
  }
  .user-avatar {
    cursor: pointer;
    width: 24px;
    height: 24px;
    border-radius: 12px;
  }
  .nickname {
    color: #475669;
    margin-left: 10px;
  }
  .middle-align {
    vertical-align: middle;
  }
  .issue-content {
    margin-top: 20px;
  }
</style>
